package com.zcf.socket;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.controller.OrderController;
import com.zcf.mapper.UserMapper;
import com.zcf.pojo.Riding;
import com.zcf.pojo.SocketContainer;

@ServerEndpoint("/echo")
@Component
@ResponseBody
@CrossOrigin
public class EchoSocket {
	@Autowired
	private UserMapper userInfoDao;
//	@Autowired
//	private AdminDao adminDao;

	// type --1登陆进去,2聊天信息,3发红包,4图片

	public EchoSocket() {
		System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
	}

	private List<Session> lists = new ArrayList<>();
	// private Map<String, Session> maps =new HashMap<>();
	// private static Set<EchoSocketFriend> sockets=new HashSet<>();
	private static Map<String, EchoSocket> echoMap = new HashMap<>();
	private static Map<String, EchoSocket> echoMapUserId = new HashMap<>();
	private static Map<String, String> mapinfo = new HashMap<String, String>();
	// private static List<SocketContainer> list = new ArrayList<>();
	private static Map<String, SocketContainer> maps = new HashMap<>();
	private static Map<String, SocketContainer> mapsUserId = new HashMap<>();

	private List<SocketContainer> list = new ArrayList<>();
	private Session session;

	@OnOpen
	public void open(Session session) {

		SocketContainer socketContainer = new SocketContainer();
		this.session = session;
		// sockets.add(this);
		echoMap.put(session.getId(), this);// sessionId主键

		System.out.println("sessionId" + session.getId());
		String str = session.getQueryString();
		String name = "";
		String userId = "";
//		if (str.length() > 0) {
//			String[] list = str.split(";");
//			if (list.length > 0) {
//				name = list[0];
//				String[] s = name.split(":");
//				userId = s[1];
//			}
//		}
		echoMapUserId.put(userId, this);// 1
//		
	}

	@OnClose
	public void close(Session session) {
		System.out.println("关闭" + session.getId());
		echoMap.remove(session.getId());
		String userId = mapinfo.get(session.getId());
		if (echoMapUserId.containsKey(userId)) {
			echoMapUserId.remove(userId);
		}
		maps.remove(session.getId());
	}

	@OnMessage
	@PostMapping("message")
	public Body message(Session session, String msg) {
		System.err.println("-------------" + msg + "---------");
		// JSONArray json1 = new JSONArray();
		org.json.JSONObject j = new org.json.JSONObject(msg);
		String toId = j.get("sendid").toString();
		System.err.println(toId);
		OrderController orderController = new OrderController();
		Body body = orderController.addDistance(toId);
		EchoSocket echoSocketFriend = echoMapUserId.get(toId);
		
		if (echoSocketFriend == null) {
			return Body.BODY_500;
		}
		try {
			echoSocketFriend.session.getBasicRemote().sendText(msg);
		} catch (IOException e) {

			e.printStackTrace();
		}
		return Body.newInstance(body);
	}

	public void sendRedBag() {

	}

	public void broadcast(Set<EchoSocket> ss, String msg) {

		for (Iterator iterator = ss.iterator(); iterator.hasNext();) {
			EchoSocket chatSocket = (EchoSocket) iterator.next();
			try {
				chatSocket.session.getBasicRemote().sendText(msg);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void broadcastA(Map<String, EchoSocket> echoMap, String msg) {
		Iterator<Entry<String, EchoSocket>> entry = echoMap.entrySet().iterator();
		while (entry.hasNext()) {
			Entry<String, EchoSocket> e = entry.next();

			EchoSocket echo = e.getValue();
			System.out.println(msg);
			try {
				if (echo.session.equals(this.session)) {
					continue;
				}
				echo.session.getBasicRemote().sendText(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}
}
